<?php

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;

class ActivityRegisterGoods extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'activity_register_goods';

    const DISCOUNT_LEVEL_GOODS = 1;
    const DISCOUNT_LEVEL_SKU = 2;

//    const discount_type  = 1;
    const DISCOUNT_TYPE_DEFAULT = 1;
    const DISCOUNT_TYPE_REDUCE_MONEY = 2;
    const DISCOUNT_TYPE_PROMOTION = 3;

    const DISCOUNT_LEVEL_DESC = [
        self::DISCOUNT_LEVEL_GOODS => '商品级',
        self::DISCOUNT_LEVEL_SKU => 'SKU级别',

    ];

    const DISCOUNT_TYPE_DESC = [
        self::DISCOUNT_TYPE_DEFAULT => '打折',
        self::DISCOUNT_TYPE_REDUCE_MONEY => '减钱',
        self::DISCOUNT_TYPE_PROMOTION => '促销价',
    ];


    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    const STATUS_DESC = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_DELETE => '已删除',
    ];

    public static function getList($where, $select = '*', $limit = [], $order = [])
    {
        $param = [
            'AND' => $where,
        ];
        if ($limit) {
            $param['LIMIT'] = $limit;
        }
        if ($order) {
            $param['ORDER'] = $order;
        }
        $list = self::select($select, $param);
        return $list;
    }

    public static function getInfos(array $registerIds, $shopId, $isFormat = true, $field = '*')
    {
        $infos = self::select($field, [
            'register_id' => $registerIds,
            'shop_id' => $shopId,
            'status' => self::STATUS_NORMAL,
        ]);

        // 查询选中日志表, 如果查到的商品id存在于日志表的话就是选中状态
        if ($isFormat) {
            $results = [];
            foreach ($infos as $info) {
                $results[$info['register_id']]['discount_level'] = $info['discount_level'];
                $results[$info['register_id']]['discount_type'] = $info['discount_type'];
                $results[$info['register_id']]['goods'][$info['goods_id']]['is_pick_up'] = (int)$info['is_pick_up'];
//                $results[$info['register_id']]['goods'][$info['goods_id']]['is_selected'] = $info['status'] == self::STATUS_SELECTED ? 1 : 0;
                $results[$info['register_id']]['goods'][$info['goods_id']]['skus'][$info['sku_id']] = [
                    'sku_id' => $info['sku_id'],
                    'discount_degree' => isset($info['discount_degree']) ? $info['discount_degree'] : 0,
                    'actual_price' => $info['actual_price'],
                ];
            }

            return $results;
        }
        return $infos;
    }

    public static function getRegisterGoods($where, $field = '*')
    {
        return self::select($field, $where);
    }

    public static function deleteRegister($registerId, $shopId)
    {
        return self::update(['status' => self::STATUS_DELETE], [
            'register_id' => $registerId,
            'shop_id' => $shopId,
        ])->rowCount();

    }

    public static function saveRegister($registerId, $shopId, $discountLevel, $discountType, $goodsList)
    {
        $currentIds = [];
        foreach ($goodsList as $goods) {
            foreach ($goods['skus'] as $skus) {
                $registerInfo = self::get('*', [
                    'register_id' => $registerId,
                    'shop_id' => $shopId,
                    'goods_id' => $goods['goods_id'],
                    'sku_id' => $skus['sku_id'],
                    'status' => self::STATUS_NORMAL
                ]);
                if ($registerInfo) {
                    $id = $registerInfo['id'];
                    self::update([
                        'discount_degree' => $skus['discount_degree'],
                        'discount_level' => $discountLevel,
                        'discount_type' => $discountType,
                        'is_pick_up' => $goods['is_pick_up'],
                        'actual_price' => $skus['actual_price'],
                        'status' => self::STATUS_NORMAL,
                    ], ['id' => $id]);
                } else {
                    self::insert([
                        'register_id' => $registerId,
                        'shop_id' => $shopId,
                        'discount_level' => $discountLevel,
                        'discount_type' => $discountType,
                        'goods_id' => $goods['goods_id'],
                        'sku_id' => $skus['sku_id'],
                        'is_pick_up' => $goods['is_pick_up'],
                        'actual_price' => $skus['actual_price'],
                        'discount_degree' => $skus['discount_degree'],
                    ]);
                    $id = self::id();
                }
                $currentIds[] = $id;
            }
        }
        $where = [
            'register_id' => $registerId,
            'shop_id' => $shopId,
        ];
        if ($currentIds) {
            $where['id[!]'] = $currentIds;
        }
        self::update(['status' => self::STATUS_DELETE], $where);
        return $currentIds;
    }

    public static function changeStatusRegisterGoods($registerId, $goodsIds)
    {
        return self::update(['status' => self::STATUS_NORMAL, 'special_id' => 0], [
            'register_id' => $registerId,
            'goods_id' => $goodsIds,
        ]);
    }
}